package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import java.time.LocalDateTime;

/**
 * 教学资源实体类
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Getter
@Setter
@ToString
@EqualsAndHashCode(of = "id")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "teaching_resources", indexes = {
        @Index(name = "idx_resource_subcategory", columnList = "subcategory_id"),
        @Index(name = "idx_resource_type", columnList = "type_id"),
        @Index(name = "idx_resource_uploader", columnList = "uploader_id"),
        @Index(name = "idx_resource_recommended", columnList = "is_recommended"),
        @Index(name = "idx_resource_created", columnList = "created_at")
})
public class TeachingResource {

    /**
     * 资源ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 子分类ID
     */
    @NotNull(message = "子分类ID不能为空")
    @Column(name = "subcategory_id", nullable = false)
    private Long subcategoryId;

    /**
     * 资源类型ID
     */
    @NotNull(message = "资源类型ID不能为空")
    @Column(name = "type_id", nullable = false)
    private Integer typeId;

    /**
     * 资源标题
     */
    @NotBlank(message = "资源标题不能为空")
    @Size(max = 200, message = "资源标题长度不能超过200个字符")
    @Column(nullable = false, length = 200)
    private String title;

    /**
     * 资源描述
     */
    @Size(max = 1000, message = "资源描述长度不能超过1000个字符")
    @Column(length = 1000)
    private String description;

    /**
     * 文件URL
     */
    @NotBlank(message = "文件URL不能为空")
    @Size(max = 500, message = "文件URL长度不能超过500个字符")
    @Column(name = "file_url", nullable = false, length = 500)
    private String fileUrl;

    /**
     * 文件大小
     */
    @NotBlank(message = "文件大小不能为空")
    @Size(max = 50, message = "文件大小长度不能超过50个字符")
    @Column(name = "file_size", nullable = false, length = 50)
    private String fileSize;

    /**
     ício     * 封面图片
     */
    @Size(max = 500, message = "封面图片路径长度不能超过500个字符")
    @Column(name = "cover_image", length = 500)
    private String coverImage;

    /**
     * 所属学科
     */
    @Size(max = 50, message = "学科名称长度不能超过50个字符")
    @Column(length = 50)
    private String subject;

    /**
     * 上传者ID
     */
    @NotNull(message = "上传者ID不能为空")
    @Column(name = "uploader_id", nullable = false)
    private Integer uploaderId;

    /**
     * 下载次数
     */
    @Builder.Default
    @Column(name = "download_count")
    private Integer downloadCount = 0;

    /**
     * 浏览次数
     */
    @Builder.Default
    @Column(name = "view_count")
    private Integer viewCount = 0;

    /**
     * 是否推荐
     */
    @Builder.Default
    @Column(name = "is_recommended")
    private Boolean isRecommended = false;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "created_at", updatable = false)
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    /**
     * 关联关系 - 资源类型
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "type_id", insertable = false, updatable = false)
    private TeachingResourceType resourceType;

    /**
     * 关联关系 - 子分类
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "subcategory_id", insertable = false, updatable = false)
    private TeachingSubcategory subcategory;

    /**
     * 增加下载次数
     */
    public void incrementDownloadCount() {
        this.downloadCount = (this.downloadCount == null) ? 1 : this.downloadCount + 1;
    }

    /**
     * 增加浏览次数
     */
    public void incrementViewCount() {
        this.viewCount = (this.viewCount == null) ? 1 : this.viewCount + 1;
    }

    /**
     * 验证资源是否有效
     */
    public boolean isValid() {
        return title != null && !title.trim().isEmpty() &&
                fileUrl != null && !fileUrl.trim().isEmpty();
    }
}
